package com.zw.yzk.kabu.view.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.CheckQrCodeModel;
import com.zw.yzk.kabu.utils.ResUtils;

import butterknife.Bind;

/**
 * Created by zhanwei on 2017/2/11.
 */

public class CheckTicketResultActivityActivity extends BaseActivity {

    @Bind(R.id.layout_check_failed)
    View checkFailedLayout;
    @Bind(R.id.failed_reason)
    TextView failedReason;
    @Bind(R.id.layout_check_success)
    View checkSuccessLayout;
    @Bind(R.id.check_result)
    TextView checkResult;
    @Bind(R.id.phone)
    TextView phone;
    @Bind(R.id.exhibition_name)
    TextView exhibitionName;
    @Bind(R.id.exhibition_time)
    TextView exhibitionTime;
    @Bind(R.id.exhibition_place)
    TextView exhibitionPlace;
    @Bind(R.id.ticket_price)
    TextView ticketPrice;
    @Bind(R.id.ticket_total_price)
    TextView totalPrice;
    @Bind(R.id.ticket_count)
    TextView ticketCount;
    @Bind(R.id.ticket_type)
    TextView ticketType;
    @Bind(R.id.ticket_id)
    TextView ticketId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_check_ticket_result;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.ticket_check_result))
                .build();
    }

    @Override
    public void initComponent() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CheckQrCodeModel model = getIntent().getParcelableExtra(ActivityConstant.CHECK_QR_CODE_RESULT);
        if (model == null || model.getCheckResult() != 1) {
//            checkFailed();
            checkFailedLayout.setVisibility(View.VISIBLE);
            checkSuccessLayout.setVisibility(View.GONE);
            if (model != null) {
                failedReason.setText(model.getCheckFailedReason());
            }
        } else {
            checkSuccess(model);
            checkFailedLayout.setVisibility(View.GONE);
            checkSuccessLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void checkSuccess(CheckQrCodeModel model) {
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.im_ticket_check_success);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        checkResult.setCompoundDrawables(null, drawable, null, null);
        checkResult.setText(R.string.ticket_check_success);

        phone.setText(model.getPhoneNumber());
        exhibitionName.setText(model.getName());
        exhibitionTime.setText(model.getExhibitionStartTime());
        exhibitionPlace.setText(model.getExhibitionAddress());
        ticketPrice.setText(ResUtils.formatCurrency(this, model.getPrice()));
        totalPrice.setText(ResUtils.formatCurrency(this, model.getTotalPrice()));
        ticketCount.setText(String.valueOf(model.getCount()));
        ticketType.setText(model.getTicketType());
        ticketId.setText(model.getId());
    }

    private void checkFailed() {
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.im_ticket_check_failed);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        checkResult.setCompoundDrawables(null, drawable, null, null);
        checkResult.setText(R.string.ticket_check_failed);
    }
}
